package flc.ast.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.SelPicActivity;
import flc.ast.adapter.RecordAdapter;
import flc.ast.databinding.FragmentEditBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.event.usersys.UserSysEventProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link EditFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class EditFragment extends BaseNoModelFragment<FragmentEditBinding> {

    private List<String> listShow = new ArrayList<>();
    private RecordAdapter recordAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_edit;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.rlPicBeauty.setOnClickListener(this);
        mDataBinding.rlPicRepair.setOnClickListener(this);
        mDataBinding.llPicCut.setOnClickListener(this);
        mDataBinding.llPicRotate.setOnClickListener(this);
        mDataBinding.llPicAdjust.setOnClickListener(this);
        mDataBinding.llPicFilter.setOnClickListener(this);
        mDataBinding.ivPicFilter.setOnClickListener(this);
        mDataBinding.rlPicPuzzle.setOnClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new GridLayoutManager(mContext, 2));
        recordAdapter = new RecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.rlPicBeauty:
            case R.id.rlPicRepair:
            case R.id.llPicCut:
            case R.id.llPicRotate:
            case R.id.llPicAdjust:
            case R.id.llPicFilter:
            case R.id.ivPicFilter:
                UserSysEventProxy.getInstance().payEvent(getActivity(), isRealOk -> {
                    if (isRealOk) {
                        SelPicActivity.type = 0;
                        startActivity(SelPicActivity.class);
                    }
                });
                break;
            case R.id.rlPicPuzzle://拼图
                UserSysEventProxy.getInstance().payEvent(getActivity(), isRealOk -> {
                    if (isRealOk) {
                        SelPicActivity.type = 1;
                        startActivity(SelPicActivity.class);
                    }
                });
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<File> files = FileUtils.listFilesInDir(PathUtils.getFilesPathExternalFirst() + Content.myEditWork);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        if (listShow.size() > 0) {
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
        }
    }

}